<template>
<div class="app-container">
    <!--工具栏-->
    <div class="head-container">
        <div v-if="crud.props.searchToggle">
            <!-- 搜索 -->
            <label class="el-form-item-label">药品名称</label>
            <el-input v-model="query.drugName" clearable placeholder="药品名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
            <rrOperation :crud="crud" />
        </div>
        <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
        <crudOperation :permission="permission" />
        <!--表单组件-->
        <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
            <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
                <el-form-item label="药品名称" prop="drugName">
                    <el-input v-model="form.drugName" style="width: 370px;" />
                </el-form-item>
                <el-form-item label="计量单位" prop="unitId">
                    <el-select v-model="form.unit.unitId" filterable placeholder="请选择">
                        <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item.unitId" />
                    </el-select>
                </el-form-item>
                <el-form-item label="售价" prop="sellingPrice">
                    <el-input v-model="form.sellingPrice" style="width: 370px;" />
                </el-form-item>
                <el-form-item label="产地" prop="area">
                    <el-select v-model="form.area.areaId" filterable placeholder="请选择">
                        <el-option v-for="item in areaList" :key="item.areaId" :label="item.areaName" :value="item.areaId" />
                    </el-select>
                </el-form-item>
                <el-form-item label="药品类型" prop="type">
                    <el-select v-model="form.type.typeId" filterable placeholder="请选择">
                        <el-option v-for="item in typeList" :key="item.typeId" :label="item.typeName" :value="item.typeId" />
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="text" @click="crud.cancelCU">取消</el-button>
                <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
            </div>
        </el-dialog>
        <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="drugId" label="药品id" />
            <el-table-column prop="drugName" label="药品名称" />
            <el-table-column prop="unitId" label="计量单位">
                <template slot-scope="scope">
                    {{ scope.row.unit.unitName }}
                </template>
            </el-table-column>
            <el-table-column prop="sellingPrice" label="售价" />
            <el-table-column prop="areaId" label="产地">
                <template slot-scope="scope">
                    {{ scope.row.area.areaName }}
                </template>
            </el-table-column>
            <el-table-column prop="typeId" label="类型">
                <template slot-scope="scope">
                    {{ scope.row.type.typeName }}
                </template>
            </el-table-column>
            <el-table-column v-if="checkPer(['admin','drugDictionary:edit','drugDictionary:del'])" label="操作" width="150px" align="center">
                <template slot-scope="scope">
                    <udOperation :data="scope.row" :permission="permission" />
                </template>
            </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination />
    </div>
</div>
</template>

<script>
import crudDrugDictionary from '@/api/his/drugDictionary'
import CRUD, {
    presenter,
    header,
    form,
    crud
} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {
    initData
} from "@/api/data";
const defaultForm = {
    drugId: null,
    drugName: null,
    unit: {
        unitId: null
    },
    sellingPrice: null,
    area: {
        areaId: null,
    },
    type: {
        typeId: null
    },
    createBy: null,
    updateBy: null,
    createTime: null,
    updateTime: null
}
export default {
    name: 'DrugDictionary',
    components: {
        pagination,
        crudOperation,
        rrOperation,
        udOperation
    },
    mixins: [presenter(), header(), form(defaultForm), crud()],
    dicts: ['user_status'],
    cruds() {
        return CRUD({
            title: 'his:药品种类清单',
            url: 'api/drugDictionary',
            idField: 'drugId',
            sort: 'drugId,desc',
            crudMethod: {
                ...crudDrugDictionary
            }
        })
    },
    data() {
        return {
            unitList: [],
            typeList: [],
            areaList: [],
            permission: {
                add: ['admin', 'drugDictionary:add'],
                edit: ['admin', 'drugDictionary:edit'],
                del: ['admin', 'drugDictionary:del']
            },
            rules: {
                drugName: [{
                    required: true,
                    message: '药品名称不能为空',
                    trigger: 'blur'
                }],
                unit: [{
                    required: true,
                    message: '计量单位不能为空',
                    trigger: 'blur'
                }],
                sellingPrice: [{
                    required: true,
                    message: '售价不能为空',
                    trigger: 'blur'
                }],
                area: [{
                    required: true,
                    message: '产地不能为空',
                    trigger: 'blur'
                }],
                type: [{
                    required: true,
                    message: '类型不能为空',
                    trigger: 'blur'
                }]
            },
            queryTypeOptions: [{
                key: 'drugName',
                display_name: '药品名称'
            }]
        }
    },
    created() {
        this.queryUnitList();
        this.queryAreaList();
        this.queryTypeList();
    },
    methods: {
        // 钩子：在获取表格数据之前执行，false 则代表不获取数据
        [CRUD.HOOK.beforeRefresh]() {
            return true
        },
        //查询所有单位
        queryUnitList() {
            initData("/api/unit")
                .then((res) => {
                    this.unitList = res.content;
                })
                .catch((error) => {});
        },
        //查询所有产地
        queryAreaList() {
            initData("/api/area")
                .then((res) => {
                    this.areaList = res.content;
                })
                .catch((error) => {});
        },
        //查询所有药品类型  
        queryTypeList() {
            initData("/api/type")
                .then((res) => {
                    this.typeList = res.content;
                })
                .catch((error) => {});
        },
        test() {
            console.log(this.form);
        }
    }
}
</script>

<style scoped>

</style>
